package com.ying.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ApiResponse;
import com.ying.po.system.Permission;
import com.ying.service.system.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/permission")
@Api(tags = "Permission接口")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @SaCheckPermission("permission-add")
    @GetMapping("/create")
    @ApiOperation("创建权限")
    public ApiResponse create(@RequestParam("perName") String perName) {
        Integer id = permissionService.createByName(perName);
        return ApiResponse.success(id);
    }

    @SaCheckPermission("permission-add")
    @PostMapping("/change/{roleCode}")
    @ApiOperation("改变某个角色的权限")
    public ApiResponse create(@RequestBody List<Integer> ids, @PathVariable Integer roleCode) {
        List<Integer> list = permissionService.changePermissionByIdList(ids, roleCode);
        return ApiResponse.success(list);
    }
    

    @SaCheckPermission("permission-get")
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(permissionService.findById(id));

    }

    @SaCheckPermission("permission-get")
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ApiResponse findPage(@RequestBody PageWrap<Permission> pageWrap) {
        return ApiResponse.success(permissionService.findAllByPage(pageWrap));
    }


    @SaCheckPermission("permission-update")
    @PutMapping("/update")
    @ApiOperation("修改权限")
    public ApiResponse updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return ApiResponse.success(null);
    }


    @SaCheckPermission("permission-delete")
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    public ApiResponse delete(@PathVariable Integer id) {
        permissionService.deleteById(id);
        return ApiResponse.success(null);
    }

    @SaCheckPermission("permission-delete")
    @DeleteMapping("/delete")
    @ApiOperation("根据ID列表批量删除")
    public ApiResponse delete(@RequestBody List<Integer> ids) {
        permissionService.deleteByIds(ids);
        return ApiResponse.success(null);
    }


    @GetMapping("/find/all")
    @SaCheckPermission("permission-get")
    @ApiOperation("查询全部的权限")
    public ApiResponse findAll() {
        ApiResponse success = ApiResponse.success(permissionService.findAll());
        return success;
    }

    @GetMapping("/find/{roleCode}")
    @SaCheckPermission("permission-get")
    @ApiOperation("用Rolecode查询权限Id")
    public ApiResponse findPermissionIdByRoleCode(@PathVariable("roleCode") Integer roleCode) {
        ApiResponse success = ApiResponse.success(permissionService.findPermissionIdByRoleCode(roleCode));
        return success;
    }


//    @PostMapping("/query")
//    @SaCheckPermission("user-get")
//    @ApiOperation("根据条件查询用户")
//    public ApiResponse queryAllByPage(@RequestBody PageWrap<User> pageWrap) {
//        return ApiResponse.success(permissionService.queryAllByPage(pageWrap));
//
//    }

}
